<template>
	<div>
		<van-address-list
		  v-model="chosenAddressId"
		  :list="list"
		  :disabled-list="disabledList"
		  default-tag-text="默认"
		  @add="onAdd"
		  @edit="onEdit"
		   @select="onSelect"
		/>
	</div>
</template>

<script>
	import { AddressList,Toast  } from 'vant';
	import API from '../../utils/API.js'
	export default {
	  data() {
	    return {
	      chosenAddressId: '1',
	      list: [
	        // {
	        //   id: '1',
	        //   name: '张三',
	        //   tel: '13000000000',
	        //   address: '浙江省杭州市西湖区文三路 138 号东方通信大厦 7 楼 501 室',
	        //   isDefault: true,
	        // },
	       
	      ],
	      disabledList: [
	      
	      ],
	    };
	  },
	  components:{
		  [AddressList.name]:AddressList,
		  [Toast.name]:Toast
	  },
	  methods: {
	    onAdd() {
	      Toast('新增地址');
		  this.$router.push({
		  	name: 'amend'
		  })
	    },
	    onEdit(item, index) {
	      Toast('编辑地址:' + index);
	    },
		async onSelect(item,index){ //切换选择地址时触发，向本地储存地址id
			// console.log(item,index)
			await this.$store.commit("newaddress",item.id)
			Toast.success('切换成功');
			localStorage.setItem('addressId', JSON.stringify(item.id))
				this.$router.push({
					path: '/gwc'
				})
				// clearTimeout(this.timer); //清除延迟执行
				// this.timer = setTimeout(()=>{  //设置延迟执行
				//   this.$router.back()
				// },1000);
		}
	  },
	  created() {
		  
	  	this.$get(API.GET_ADDRESS_LIST,{
			token:this.$store.state.token
		})
		.then(res => {
			// console.log(res.data.data.list)
			for(let i of res.data.data.list){
				if(i.isDefault){
					this.chosenAddressId = i.id
				}
				i.address = i.province + i.city + i.county + i.addressDetail; 
			}
			// for(let i=0;i,res.data.data.list.length;i++){
			// 	//  this.chosenAddressId =i.id
			// 	i.address = i.province + i.city + i.county + i.addressDetail;
				
			// }
			this.list=res.data.data.list
			console.log(this.list)
		})
	  }
	};
</script>

<style>
</style>
